package com.ruif.client;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.ruif.dao.TemperaDAO;
import com.ruif.model.Tempera;
import com.ruif.service.ITemperaService;
import com.ruif.util.DBUtil;
import com.ruif.util.StringUtil;

/**
 * ITemperaService 接口实现类(业务逻辑层)
 * @author pc
 *
 */
public class TemperaServiceImpl implements ITemperaService
{
    TemperaDAO tempDAO = new TemperaDAO();
    DBUtil db = new DBUtil();
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public int list(DefaultTableModel dtm, Tempera tem)
    {
        Connection conn = null;
        try
        {
            conn = db.getConn();
            ResultSet rs = tempDAO.list(conn, tem);
            while (rs.next())
            {
                Vector v = new Vector();
                v.add(rs.getLong("id") + "");
                v.add(rs.getInt("temp") + "");
                v.add(rs.getString("date"));
                v.add(rs.getInt("status") == 0 ? "正常" : "报警");
                dtm.addRow(v);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                db.closeConn(conn);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return 0;
    }

    public int add(Tempera tem)
    {
        if (tem == null)
        {
            JOptionPane.showMessageDialog(null, "请输入信息！");
            return 0;
        }
        if (StringUtil.isEmpty(tem.getTemp() + ""))
        {
            JOptionPane.showMessageDialog(null, "请输入温度！");
            return 0;
        }
        Connection conn = null;
        try
        {
            conn = db.getConn();
            return tempDAO.add(conn, tem);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "连接数据库失败！");
        }
        finally
        {
            try
            {
                db.closeConn(conn);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return 0;   //返回0表示操作失败
    }

    public int delete(String id)
    {
        if (StringUtil.isEmpty(id + ""))
        {
            JOptionPane.showMessageDialog(null, "请选择要删除的记录！");
            return 0;
        }
        
        Connection conn = null;
        try
        {
            conn = db.getConn();
            return tempDAO.delete(conn, id);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "数据库连接失败！");
        }
        finally
        {
            try
            {
                db.closeConn(conn);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return 0;
    }

    public int modify(Tempera tem)
    {
        if (tem == null)
        {
            JOptionPane.showMessageDialog(null, "请选择要修改的记录！");
            return 0;
        }
        if (StringUtil.isEmpty(tem.getId() + ""))
        {
            JOptionPane.showMessageDialog(null, "请选择要修改的记录！");
            return 0;
        }
        
        Connection conn = null;
        try
        {
            conn = db.getConn();
            return tempDAO.modify(conn, tem);//如果成功返回1
        }
        catch (Exception e)
        {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "修改失败！");
        }
        finally
        {
            try
            {
                db.closeConn(conn);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return 0;
    }
    
}
